package com.aliyun.sample;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

public class MonitorUtil {
    public static Options buildCommandlineOptions(final Options options) {


        Option option = new Option("ak","accessKey", true, "access key");
        option.setRequired(false);
        options.addOption(option);

        option = new Option("sk","accessKeySecret", true, "access key secret");
        option.setRequired(false);
        options.addOption(option);

        option = new Option("ep","endPoint", true, "endpoint");
        option.setRequired(false);
        options.addOption(option);

        option = new Option("gid","groupId", true, "groupid");
        option.setRequired(false);
        options.addOption(option);

        option = new Option("iid", "instanceId", true, "configuration file");
        option.setRequired(false);
        options.addOption(option);

        option = new Option("f","configfile", true, "");
        option.setRequired(false);
        options.addOption(option);

        option = new Option("gw","pushGatewayUrl", true, "prometheus pushgateway url");
        option.setRequired(false);
        options.addOption(option);

        return options;
    }
    public static Properties commandLine2Properties(CommandLine commandLine) {
        Properties properties = new Properties();
        Option [] options = commandLine.getOptions();
        if(options != null) {
            for(Option option : options) {
                String name = option.getLongOpt();
                String value = commandLine.getOptionValue(name);
                if(value != null) {
                    properties.setProperty(name, value);
                }
            }
        }
        return properties;
    }
    public static void properties2Object(final Properties p, final Object object) {
        Method[] methods = object.getClass().getMethods();
        for (Method method : methods) {
            String mn = method.getName();
            if (mn.startsWith("set")) {
                try {
                    String tmp = mn.substring(4);
                    String first = mn.substring(3, 4);

                    String key = first.toLowerCase() + tmp;
                    String property = p.getProperty(key);
                    if (property != null) {
                        Class<?>[] pt = method.getParameterTypes();
                        if (pt != null && pt.length > 0) {
                            String cn = pt[0].getSimpleName();
                            Object arg = null;
                            if (cn.equals("int") || cn.equals("Integer")) {
                                arg = Integer.parseInt(property);
                            } else if (cn.equals("long") || cn.equals("Long")) {
                                arg = Long.parseLong(property);
                            } else if (cn.equals("double") || cn.equals("Double")) {
                                arg = Double.parseDouble(property);
                            } else if (cn.equals("boolean") || cn.equals("Boolean")) {
                                arg = Boolean.parseBoolean(property);
                            } else if (cn.equals("float") || cn.equals("Float")) {
                                arg = Float.parseFloat(property);
                            } else if (cn.equals("String")) {
                                arg = property;
                            } else {
                                continue;
                            }
                            method.invoke(object, arg);
                        }
                    }
                } catch (Throwable ignored) {
                }
            }
        }
    }

}
